﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Zhang.Utils
{
    public class StringHelper
    {
        /// <summary>
        /// 页面标题，讲空格等特殊字符换成“-”
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string TitleReplace(string title)
        {
            var str = Regex.Replace(title, "[^a-zA-Z0-9]", "-");
            str = Regex.Replace(str, "--+", "-");
            //TELUKBETUNG(BANDAR LAMPUNG)
            //TELUKBETUNG-BANDAR LAMPUNG-
            if (str.Substring(str.Length - 1, 1) == "-")
                str = str.Substring(0, str.Length - 1);
            return str;
        }

        /// <summary>
        /// 将字符中的空格，换成指定符号
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string strReplace(string sstr,string s1,string s2)
        {
            var str = Regex.Replace(sstr, s1, s2);
            return str;
        }

        public static string PortName(string str)
        {
            if (str.Contains(","))
            {
                return str?.Substring(0, str.IndexOf(","));
            }
            else if (str.Contains("，"))
            {
                return str?.Substring(0, str.IndexOf("，"));
            }
            else
            { return str; }
        }

        /// <summary>
        /// 把decimal转为int
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ConversionInt(decimal str)
        {
            try
            {
                if (str==Convert.ToInt32(str))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {

                return true;
            }

         
        }
    }
}
